﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microhard.Permission.Validator;
using Microhard.Core.DBUtility;

namespace Microhard.Permission.Models
{
    /// <summary>
    /// 程琳 2012-10-7
    /// </summary>
    [FluentValidation.Attributes.Validator(typeof(PermissionValidator))]
    public class Permission : BasePermissionModel
    {
        /// <summary>
        /// 授权ID号
        /// </summary>
        public Guid PermissionID { set; get; }
        /// <summary>
        /// 授权角色ID号或用户ID号
        /// </summary>
        public Guid OwnerID { set; get; }

        public string OwnerName { set; get; }

        /// <summary>
        /// 决定授权给角色还是用户（1代表用户，2代表角色）
        /// </summary>
        public int OwnerType { set; get; }
        /// <summary>
        /// 类型名称
        /// </summary>
        public string OwnerTypeName
        {
            get
            {
                if (OwnerType == (int)OwnerIDType.User)
                    return "用户";
                if (OwnerType == (int)OwnerIDType.Role)
                    return "角色";
                return string.Empty;
            }
        }
        /// <summary>
        /// 资源ID
        /// </summary>
        public Guid ResID { set; get; }
        /// <summary>
        /// 资源名称
        /// </summary>
        public string ResourceName { set; get; }
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime AssignDateTime { set; get; }
        /// <summary>
        /// 赋予的Operation
        /// </summary>
        public List<Operation> AssignOperations { set; get; }

        public string OperationName
        {
            get
            {
                StringBuilder display = new StringBuilder();
                if (this.AssignOperations != null)
                {
                    foreach (var r in this.AssignOperations)
                    {
                        display.Append(r.Name + ",");
                    }
                }
                return display.ToString();
            }
        }
        /// <summary>
        /// 数据权限
        /// </summary>
        public int Scope
        {
            get
            {
                int rtn = -1;
                if (int.TryParse(this.ScopeForSelect, out rtn))
                {
                    return rtn;
                }
                return rtn;
            }
        }

        public string ScopeForSelect { set; get; }

        public string ScopeDisplyName { set; get; }
        /// <summary>
        /// 组范围
        /// </summary>
        public string BelongGroup { set; get; }

        public List<SpecificScope> SpecificScopes { set; get; }

        //数据库映射使用
        public Guid OperID { set; get; }
        public bool Status { set; get; }
        public string ResourceKeyName { set; get; }
        public string OperationKeyName { set; get; }
    }


    public class SpecificScope
    {
        public Guid EntityID { set; get; }
        public string EntityName { set; get; }
        public OwnerIDType EntityType { set; get; }
    }

}
